// playTicTacToe.cpp
// This set of functions are used to actually play the game
// Herson 
#include "nim.h"
#include <WinSock2.h>
#include <iostream>
#include <string>

using namespace std;

#define SECONDS_FOR_PLAYER_TO_RESPOND 20
#define MAX_NUM_DIGITS_FOR_PLAYER_INTEGER 15
#define MOVE_LENGTH 2


/* std::string updateBoard( std::string GameBoard, int move, int player)
{
	int nPiles = GameBoard[0];
	
		if ( row > nPiles)
		{
			//Improper move
			std::cout<<"Improper Move, please try again"<<std::endl;
		}
		else if(GameBoard[row -1] < manyHow)
		{
			//improper pull from 
			std::cout<<"Improper move, please try again"<<std::endl;
		}

		GameBoard[row-1]  = GameBoard[row-1] -manyHow;

		if(row==1 && GameBoard[row-1]==0)
		{
			return "L";
		}
}*/

void displayBoard( std::string board )
{
	std::cout << std::endl;
	int nPiles = (board[0])-48;
	for (int i = 0; i < nPiles; i++)
	{
		std::cout<<"Pile #:"<<i+1;
		std::string num = board.substr(i*2+1,2);
		int nRocks = atoi(num.c_str());
		for(int j=0; j < nRocks; j++)
		{
			std::cout<<" * ";
		}
		std::cout<<std::endl;
	}
	std::cout << std::endl;
}


void sendMove(SOCKET s,std::string moveString)
{
	char * move = new char[moveString.length()+1];
	move[moveString.length()]= '\0';
	strcpy_s(move,moveString.length()+1,moveString.c_str());
	TCP_send(s,move,moveString.length()+1);
	delete [] move;
}
std::string getMove(SOCKET s, std::string GameBoard)
{
	long long row;
	long long pileNumRocks;

	std::cout << "What row do you want to pull from?";
	std::cin>>row;

	
	std::cout << "How many ?";
	std::cin  >> pileNumRocks;
		
	//check move

	int nPiles = GameBoard[0]-48;
	
	if ( row > nPiles)
	{
		//Improper move
		std::cout<<"Improper Move, please try again"<<std::endl;
		return "C";
	}
	std::string num = GameBoard.substr(row,2);
	int howMany = atoi(num.c_str());
	if(howMany < pileNumRocks)
	{
		//improper pull from 
		std::cout<<"Improper move, please try again"<<std::endl;
		return "C";
	}

	int newManyHow = (howMany - pileNumRocks);

	if(newManyHow == 0)
	{
		GameBoard[0] = ((GameBoard[0] -48) -1) +48;
		GameBoard[row] = '0';
		GameBoard[row+1] = '0';
	}

	std::string move = to_string(row);
	if(pileNumRocks<10)
	{
		move+="0";
	}
	move+= to_string(pileNumRocks);

	if(GameBoard[0] -48==1 && newManyHow==0)
	{
		return "L";
	}else {
		sendMove(s, move);
	}
	

	return GameBoard;
}

void sendComment(SOCKET s)
{
	std::cout<< "Insert comment"<<std::endl;

	std::string moveString;
	std::cin>>moveString;

	moveString = "C" + moveString;
	char * move = new char[moveString.length()+1];
	strcpy_s(move,moveString.length()+1,moveString.c_str());
	TCP_send(s,move,moveString.length()+1);
	delete [] move;
}


std::string promptUser(SOCKET s,std::string board)
{
	std::string input;
	std::cout<<"Press F(Forfeit), C(Comment), or M(Move)"<<std::endl;
	std::cin>>input;
	if(input == "F")
	{
		input = "L";
	}else if(input == "C")
	{
		sendComment(s);
	}else{
		board = getMove(s,board);
		if (board == "L")
		{
			input = "L";
		}else if(board =="C")
		{
			input = "C";
		}
	}
	return input;
}
std::string applyOpponentMove(std::string board ,std::string move)
{
	int pile = move[0] -48;
	int howManyToPull = atoi(move.substr(1).c_str());
	
	int numRocksAtPile = atoi(move.substr(pile,2).c_str());
	if (board[0] -48 < pile)
	{
		return "W";
	}else if (numRocksAtPile < howManyToPull)
	{
		return "W";
	}if (board[0] -48 == 1 && numRocksAtPile == howManyToPull)
	{
		return "W";
	}else{
		int newNumRocksAtPile = (numRocksAtPile - howManyToPull);
		if(newNumRocksAtPile < 10)
		{
			board[pile] = '0';
			board[pile+1] = newNumRocksAtPile;
		}else {
			
			board[pile]  =(newNumRocksAtPile/10)+48;
			board[pile+1]=(newNumRocksAtPile-10)+48;
		}
		return board;
	}
}
int playNim(SOCKET s, char *serverName, char *host, char *port, int player,std::string boardSetup)
{
	// This function plays the game and returns the value: winner.  This value 
	// will be one of the following values: noWinner, xWinner, oWinner, TIE, ABORT
	int winner = noWinner;
	std::string board;
	int opponent;
	
	bool myMove;

	if (player == CLIENT_PLAYER) {
		std::cout << "Playing as Client" << std::endl;
		opponent = SERVER_PLAYER;
		myMove = true;
	} else {
		std::cout << "Playing as Server" << std::endl;
		opponent = CLIENT_PLAYER;
		myMove = false;
	}

	
	displayBoard(boardSetup);

	while (winner == noWinner) {
		if (myMove) {
			std::string input = "";
			do {
				input = promptUser(s,boardSetup);
			} while(input=="C");
			
			std::cout << "Board after your move:" << std::endl;
			displayBoard(boardSetup);
			if (input == "L")
			{
				winner = opponent;

			}else{
				displayBoard(boardSetup);
				std::string moveString;
				std::cin>>moveString;
				char * move = new char[moveString.length()+1];
				move[moveString.length()]= '\0';
				strcpy_s(move,moveString.length()+1,moveString.c_str());
				TCP_send(s,move,moveString.length()+1);
				delete [] move;
			}

		} else {
			std::cout << "Waiting for your opponent's move..." << std::endl << std::endl;
			//Get opponent's move & display board
			
			char hostOpponent[MAX_HOST_SIZE];
			char port[MAX_SERVICE_SIZE];

			char opponentMove[MAX_TCP_MESSAGE];
			int lengthReceived = TCP_recv(s,opponentMove,MAX_TCP_MESSAGE,SECONDS_FOR_PLAYER_TO_RESPOND);
			
			if ( lengthReceived > 0)
			{
				if( opponentMove[0] == 'F')
				{
					winner = player;
				}
				else if ( opponentMove[0] == 'C')
				{
					std::cout<<"Comment:"<<std::endl;
					for(int i=1;i < lengthReceived-1; ++i)
					{
						std::cout<<opponentMove[i];
					}
					std::cout<<std::endl;
					// keep waiting for a move
					myMove = !myMove;
				}else {
					std::string opponentMoveString = opponentMove;
					std::string message = applyOpponentMove(boardSetup,opponentMoveString);
					if(message == "W")
					{
						winner = player;
					}else {
						boardSetup = message;
					}

					displayBoard(board);
				}
			}

	
			
		}
		
		myMove = !myMove;
	}
	
	if (winner == player)
		std::cout << "You WIN!" << std::endl;
	else if (winner == TIE)
		std::cout << "It's a tie." << std::endl;
	else if (winner == opponent)
		std::cout << "I'm sorry.  You lost" << std::endl;
		
	return winner;
}